package com.jh.netty.instruction.datapacket;

import com.jh.netty.instruction.DataPacket;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 门参数表
 *
 * @see com.jh.netty.instruction.DataPacketCMD#CMD_GET_DOOR_CONFIG 获取门参数表
 *
 * <br>
 * 门状态参数表长每门 40 bytes。共 160 byte
 * <br>
 *
 * <ul>
 *     <li>00 00 00 02 00 00 00 05 1e 00 00 00 00 00 00 00 00 00 00 12 34 56 78 05 00 000000000000000000000000000000</li>
 *     <li>00 00 00 02 00 00 00 05 1e 00 00 00 00 00 00 00 00 00 00 12 34 56 78 05 00 000000000000000000000000000000</li>
 *     <li>00000002000000051e00000000000000000000123456780500000000000000000000000000000000</li>
 *     <li>00000002000000051e00000000000000000000123456780500000000000000000000000000000000</li>
 * </ul>
 *
 * 五. 数据格式：门参数表
 */
@Data
public class DoorParam {
    /**
     * 门类型 1 byte
     * 0 - 单向读卡器，1 - 双向读卡器
     */
    private String doorType;

    /**
     * 读卡器类型: 1 byte; 0 - 自动检测，1 - WIEGAND 26，2 - WIEGAND 32
     */
    private String cardReaderType;

    /**
     * 读卡器指示灯极性: 1 byte; 0 - 阴极，1 - 阳极
     */
    private String cardReaderIndicatorPolarity;

    /**
     * 读卡间隔时间: 1 byte; 0 - 255 秒
     */
    private String readCardInterval;

    /**
     * 出门按钮类型: 1 byte; 0 - 常开，1 - 常闭，2 - 无效
     */
    private String outBtnType;

    /**
     * 门磁类型: 1 byte; 0 - 常开，1 - 常闭，2 - 无效
     */
     private String gateMagnetType;

    /**
     * 门锁类型: 1 byte; 0 - 连续，1 - 触发，2 - 无效
     */
    private String doorLockType;

    /**
     * 读卡开门时间: 1 byte; 0 - 255 秒
     */
    private String readCardOpenTime;

    /**
     * 门开超时报警时间: 1 byte; 0 - 255 秒
     */
    private String doorOpenTimeoutAlarmTime;

    /**
     * 连锁门地址组合: 1 byte; 0 - 无效，1..4 - 1..4 号门
     */
    private String chainGateAddressCombination;

    /**
     * 读卡方式: 1 byte; 0=单卡确认,1=多卡确认,2=主副卡确认
     */
    private String cardReadingMethod;

    /**
     * 读卡确认数量: 1 byte; 2 - 8
     */
    private String readCardConfirmationQuantity;

    /**
     * 双向 APB 检测: 1 byte; 0 - 无效，1 - 有效
     */
    private String bidirectionalAPBDetection;

    /**
     * 区域 APB 检测: 1 byte; 0 - 无效，1 - 有效
     */
    private String areaAPBDetection;

    /**
     * 进入区域号: 1 byte; 0 - 31 (0 = 清除)
     */
    private String entryAreaNumber;

    /**
     * 退出区域号: 1 byte; 0 - 31 (0 = 清除)
     */
    private String exitZoneNumber;

    /**
     * 首卡开门: 1 byte; 0 - 无效，1 - 有效
     */
    private String firstCardOpen;

    /**
     * 协迫密码 BCD: 2 byte; 0001 - 9999(0000 - 无效)
     */
    private String coercivePasswordBCD;

    /**
     * 超级密码 BCD: 4 byte; 00000001 - 99999999(00000000 - 无效)
     */
    private String superPasswordBCD;

    /**
     * 按钮开门时间: 1 byte; 0 - 255 秒
     */
    private String buttonOpenTime;

    /**
     * 门锁回锁时间: 1 byte; 0 - 255 秒
     */
    private String doorLockBackTime;

    /**
     * 保留： 15 bytes;
     */
    private String retain;

    public static List<DoorParam> parse(DataPacket dp) {
        return parse(dp.getDataCodeText());
    }

    /**
     * 解析门参数
     * @param doorParamStr 门参数
     */
    public static List<DoorParam> parse(String doorParamStr) {
//        int len = doorParamStr.length() / 80;

        String door1Param = doorParamStr.substring(0, 80);
        String door2Param = doorParamStr.substring(80, 160);
        String door3Param = doorParamStr.substring(160, 240);
        String door4Param = doorParamStr.substring(240, 320);
        List<DoorParam> doorParams = new ArrayList<>();

        doorParams.add(p(door1Param));
        doorParams.add(p(door2Param));
        doorParams.add(p(door3Param));
        doorParams.add(p(door4Param));

        return doorParams;
    }

    private static DoorParam p(String doorParamStr) {
        DoorParam dp = new DoorParam();
        dp.doorType = doorParamStr.substring(0, 2);
        dp.cardReaderType = doorParamStr.substring(2, 4);
        dp.cardReaderIndicatorPolarity = doorParamStr.substring(4, 6);
        dp.readCardInterval = doorParamStr.substring(6, 8);
        dp.outBtnType = doorParamStr.substring(8, 10);
        dp.gateMagnetType = doorParamStr.substring(10, 12);
        dp.doorLockType = doorParamStr.substring(12, 14);
        dp.readCardOpenTime = doorParamStr.substring(14, 16);
        dp.doorOpenTimeoutAlarmTime = doorParamStr.substring(16, 18);
        dp.chainGateAddressCombination = doorParamStr.substring(18, 20);
        dp.cardReadingMethod = doorParamStr.substring(20, 22);
        dp.readCardConfirmationQuantity = doorParamStr.substring(22, 24);
        dp.bidirectionalAPBDetection = doorParamStr.substring(24, 26);
        dp.areaAPBDetection = doorParamStr.substring(26, 28);
        dp.entryAreaNumber = doorParamStr.substring(28, 30);
        dp.exitZoneNumber = doorParamStr.substring(30, 32);
        dp.firstCardOpen = doorParamStr.substring(32, 34);
        dp.coercivePasswordBCD = doorParamStr.substring(34, 38);
        dp.superPasswordBCD = doorParamStr.substring(38, 46);
        dp.buttonOpenTime = doorParamStr.substring(46, 48);
        dp.doorLockBackTime = doorParamStr.substring(48, 50);
        return dp;
    }
}
